home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / simplerd.lha / simplerad / FinalFTP / Light / trans.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  5.8 KB  |  167 lines

  1. /**********************************************************************/
  2. /* trans.c                                                            */
  3. /*                                                                    */
  4. /* Transformations of rays, hits, planes, and bounding boxes          */
  5. /* Modified from Optik v(1.2e) (C) 1987 John Amanatides & Andrew Woo  */
  6. /*                                                                    */
  7. /* Copyright (C) 1992, Bernard Kwok                                   */
  8. /* All rights reserved.                                               */
  9. /* Revision 1.0                                                       */
  10. /* May, 1992                                                          */
  11. /**********************************************************************/
  12. #include <stdio.h>
  13. #include "geo.h"
  14. #include "misc.h"
  15. #include "struct.h"
  16. #include "ray.h"
  17.  
  18. /**********************************************************************/
  19. void RayTransform();
  20. Plane PlaneTransform();
  21. void HitTransform();
  22. BoundingBoxType BoundingBoxTransform();
  23.  
  24. /**********************************************************************/
  25. /* Transform a ray                                                    */
  26. /**********************************************************************/
  27. void RayTransform(old, new, m)
  28.      register Ray *old, *new;
  29.      register Matrix m;
  30. {
  31.   new->origin.x= old->origin.x*m[0][0] + old->origin.y*m[1][0] + 
  32.     old->origin.z*m[2][0] + m[3][0];
  33.   new->origin.y= old->origin.x*m[0][1] + old->origin.y*m[1][1] + 
  34.     old->origin.z*m[2][1] + m[3][1];
  35.   new->origin.z= old->origin.x*m[0][2] + old->origin.y*m[1][2] + 
  36.     old->origin.z*m[2][2] + m[3][2];
  37.  
  38.   new->dir.x= old->dir.x*m[0][0] + old->dir.y*m[1][0] + old->dir.z*m[2][0];
  39.   new->dir.y= old->dir.x*m[0][1] + old->dir.y*m[1][1] + old->dir.z*m[2][1];
  40.   new->dir.z= old->dir.x*m[0][2] + old->dir.y*m[1][2] + old->dir.z*m[2][2];
  41. }
  42.  
  43. /**********************************************************************/
  44. /* Transform a plane (not used)                                       */
  45. /**********************************************************************/
  46. Plane PlaneTransform(old, m)
  47.      Plane old;
  48.      register Matrix m;
  49. {
  50.   Plane new;
  51.  
  52.   new.n.x= old.n.x*m[0][0] + old.n.y*m[0][1] + old.n.z*m[0][2];
  53.   new.n.y= old.n.x*m[1][0] + old.n.y*m[1][1] + old.n.z*m[1][2];
  54.   new.n.z= old.n.x*m[2][0] + old.n.y*m[2][1] + old.n.z*m[2][2];
  55.   new.d= old.n.x*m[3][0] + old.n.y*m[3][1] + old.n.z*m[3][2] + old.d;
  56.   return(new);
  57. }
  58.  
  59. /**********************************************************************/
  60. /* Transform a hit -need if transformed the ray for intersection tests*/
  61. /**********************************************************************/
  62. void HitTransform(hit, M, iM)
  63.      register HitData *hit;          /* Hit data */
  64.      register Matrix M, iM;          /* Matrix, and inverse matrix */
  65. {
  66.   Vector old;
  67.   
  68.   /* Transform intersection point */
  69.   old= hit->intersect;
  70.   hit->intersect.x= old.x*M[0][0] + old.y*M[1][0] + old.z*M[2][0] + M[3][0];
  71.   hit->intersect.y= old.x*M[0][1] + old.y*M[1][1] + old.z*M[2][1] + M[3][1];
  72.   hit->intersect.z= old.x*M[0][2] + old.y*M[1][2] + old.z*M[2][2] + M[3][2];
  73.  
  74.   /* Transform normal at intersection */
  75.   old= hit->normal;
  76.   hit->normal.x= old.x*iM[0][0] + old.y*iM[0][1] + old.z*iM[0][2];
  77.   hit->normal.y= old.x*iM[1][0] + old.y*iM[1][1] + old.z*iM[1][2];
  78.   hit->normal.z= old.x*iM[2][0] + old.y*iM[2][1] + old.z*iM[2][2];
  79.   
  80.   /* Normalize normal */
  81.   if(norm(&hit->normal) < 0.0)
  82.     printf("\tGuess what? HitTransform had a very small normal!!\n");
  83. }
  84.  
  85. /**********************************************************************/
  86. /* Transform a bounding box                                           */
  87. /**********************************************************************/
  88. BoundingBoxType BoundingBoxTransform(box, M)
  89.      BoundingBoxType box;
  90.      register Matrix M;
  91. {
  92.   BoundingBoxType nBox;
  93.   Vector vert[8];
  94.   register int i;
  95.   
  96.   for(i=0;i<8;i++) {
  97.     if(i & 1) vert[i].x= box.max.x;
  98.     else vert[i].x= box.min.x;
  99.     if(i & 2) vert[i].y= box.max.y;
  100.     else vert[i].y= box.min.y;
  101.     if(i & 4) vert[i].z= box.max.z;
  102.     else vert[i].z= box.min.z;
  103.   }
  104.   
  105.   for(i=0;i<8;i++) 
  106.     vert[i]= vtransform(vert[i], M);
  107.   
  108.   nBox.min.x= nBox.max.x= vert[0].x;
  109.   nBox.min.y= nBox.max.y= vert[0].y;
  110.   nBox.min.z= nBox.max.z= vert[0].z;
  111.   for(i=1; i<8; i++) {
  112.     if(vert[i].x < nBox.min.x)      nBox.min.x= vert[i].x;
  113.     if(vert[i].x > nBox.max.x)      nBox.max.x= vert[i].x;
  114.     
  115.     if(vert[i].y < nBox.min.y)      nBox.min.y= vert[i].y;
  116.     if(vert[i].y > nBox.max.y)      nBox.max.y= vert[i].y;
  117.     
  118.     if(vert[i].z < nBox.min.z)      nBox.min.z= vert[i].z;
  119.     if(vert[i].z > nBox.max.z)      nBox.max.z= vert[i].z;
  120.   }
  121.  
  122.   /* Make the box a little bit larger */
  123.   nBox.min.x -= VERY_SMALL;
  124.   nBox.max.x += VERY_SMALL;
  125.   nBox.min.y -= VERY_SMALL;
  126.   nBox.max.y += VERY_SMALL;
  127.   nBox.min.z -= VERY_SMALL;
  128.   nBox.max.z += VERY_SMALL;
  129.   return(nBox);
  130. }
  131.  
  132. /**********************************************************************/
  133. /* Initialize ray data */
  134. /**********************************************************************/
  135. void RayInit(id, orig, dir, vis, gener, shad, nray)
  136.      long id;
  137.      Vector orig, dir;
  138.      double vis;
  139.      int gener, shad;
  140.      Ray *nray;
  141. {
  142.   nray->rayID = id;
  143.   nray->origin = orig;
  144.   nray->dir = dir;
  145.   nray->visible = vis;
  146.   nray->generation = gener;
  147.   nray->shadow = shad;
  148.   nray->transmissivity = (Transmit *)NULL;
  149. }
  150.  
  151. /**********************************************************************/
  152. /* Initialize hit data */
  153. /**********************************************************************/
  154. void HitInit(dist, inter, text, nrm, nhit)
  155.      double dist;    
  156.      Vector inter, text, nrm;
  157.      HitData *nhit;
  158. {
  159.   nhit->obj = (Objectt *)NULL;
  160.   nhit->poly = (Polygon *)NULL;
  161.   nhit->mesh = (Mesh *)NULL;
  162.   nhit->distance = dist;
  163.   nhit->intersect = inter;
  164.   nhit->texture = text;
  165.   nhit->normal = nrm;
  166. }
  167.